#include "edaHC.h"

#ifdef _DEBUG_
#include <iostream>
using namespace std;
#endif

edaHC::edaHC(edaMove *_move, edaMoveGen *_moveNext, edaMoveSelect *_moveSelect)
{
  move = _move->clone();
  moveExpl = new edaHCMoveExpl( _moveNext, _moveSelect );
}

edaHC::edaHC(edaMove *_move, edaHCMoveExpl *_moveExpl)
{
  move = _move->clone();
  moveExpl = new edaHCMoveExpl( *_moveExpl );
}

edaHC::~edaHC()
{
  delete move;
  delete moveExpl;
}

bool edaHC::search(edaSolution &sol)
{
  edaSolution *newSol = NULL;
  newSol = sol.clone();

  do
  {
    *newSol = sol;
    //printDebug(5, "sol: " << sol);
    //printDebug(5, "newSol: " << sol);

    moveExpl->explore(move, sol, *newSol);

    //printDebug(5, "newSol.eval: " << newSol->fullEvaluate());
    //printDebug(5, "sol.eval: " << sol.fullEvaluate());
    //printDebug(5, "newSol: " << (*newSol));
    //printDebug(5, "sol (before): " << sol);

    if (newSol->fullEvaluate() > sol.fullEvaluate())
    {
      sol = *newSol;
    }
    else
    {
      break;
    }
    //printDebug(5, "sol (after): " << sol);
    printDebug(5, "new fitness : " << sol.fullEvaluate() );
  } while (true);

  delete newSol;
  return true;
}

void edaHC::Serialize(edaBuffer &buf, bool pack)
{
  if (pack)
  {
    moveExpl->doSerialize(buf, pack);
    move->doSerialize(buf, pack);
  }
  else
  {
    moveExpl = (edaHCMoveExpl*)classGenerateFromBuffer( buf );
    move = (edaMove*)classGenerateFromBuffer( buf );
  }
}
